package com.learn.cn.useMq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/** @author admin 2019/12/12-9:35 */
public class JmsProducer {
  /*默认连接用户名*/
  private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
  /* 默认连接密码*/
  private static final String PASSWORLD = ActiveMQConnection.DEFAULT_PASSWORD;
  /* 默认连接地址*/
  private static final String BROKENURL = ActiveMQConnection.DEFAULT_BROKER_URL;
  private static final int SENDNUM = 3;

  public static void main(String[] args) {
    /* 连接工厂*/
    ConnectionFactory connectionFactory;
    /* 连接*/
    Connection connection = null;
    /* 会话*/
    Session session;
    /* 消息的目的地*/
    Destination destination;
    /* 消息的生产者*/
    MessageProducer messageProducer;

    /* 实例化连接工厂*/
    connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORLD, BROKENURL);

    /* 通过连接工厂获取连接*/
    try {
      connection = connectionFactory.createConnection();

      /* 启动连接*/
      connection.start();

      /* 创建session
       * 第一个参数表示是否使用事务，第二次参数表示是否自动确认*/
      session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      /* 创建一个名为HelloWorld消息队列*/
     destination = session.createQueue("HelloWorld");
      //destination = session.createTopic("HelloWorldTopic");

      /* 创建消息生产者*/
      messageProducer = session.createProducer(destination);

      /* 循环发送消息*/
      for (int i = 0; i < SENDNUM; i++) {
        String msg = "发送消息 " + i + "条，" + System.currentTimeMillis();
        TextMessage textMessage = session.createTextMessage(msg);
        System.out.println(msg);
        messageProducer.send(textMessage);
      }
    } catch (JMSException e) {
      e.printStackTrace();
    }finally{
      if (connection != null) {
        try {
          connection.close();
        } catch (JMSException e) {
          e.printStackTrace();
        }
      }

    }
  }
}
